package com.xingdata.jjxc.activity;

import jar.MessageManager;
import jar.model.RequestEntity;
import jar.tools.InstructionSet;
import jar.tools.UdpTools;
import com.alibaba.fastjson.JSONObject;

import android.R.integer;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.base.BaseActivity;
import com.xingdata.jjxc.utils.SystemInfo;

public class CorrectSpeedActivity extends BaseActivity {
	
	private EditText correctSpeedK;
	private EditText correctSpeedA;
	Float speedA;
	Float speedK;
	@Override
	protected int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_correctspeed;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return "车速矫正";
	}

	@Override
	protected void initViews() {
		// TODO Auto-generated method stub

		((ImageView) findViewById(R.id.title_back))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});

		correctSpeedK = (EditText) findViewById(R.id.k_value_et);
		correctSpeedA = (EditText) findViewById(R.id.a_value_et);

		Button confirmButton = (Button) findViewById(R.id.btn_sure);

		confirmButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				if (UdpTools.isContent) {

					if(TextUtils.isEmpty(correctSpeedK.getText()) || TextUtils.isEmpty(correctSpeedA.getText())) {
						Toast.makeText(getApplicationContext(), "请输入数值", Toast.LENGTH_SHORT).show();
						return;
					} else {
						
						try {
							speedK = Float.valueOf(correctSpeedK.getText().toString());
							speedA = Float.valueOf(correctSpeedA.getText().toString());
						} catch (Exception e) {
							// TODO: handle exception
							System.out.print("valueError = " + e);
						}
					
					}
					
					if (speedK <= 0 || speedK > 2) {
						Toast.makeText(getApplicationContext(), "请输入一个大于0且小于2的数值", Toast.LENGTH_SHORT).show();
						return;
					}
					
					if (speedA < -10 || speedA > 10) {
						Toast.makeText(getApplicationContext(), "请输入一个介于正负10之间的数值", Toast.LENGTH_SHORT).show();
						return;
					}
					
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("checkSpeedK",speedK);
					jsonObject.put("checkSpeedA",speedA);
					
					final int checkSpeedType = 13110;
					// 发送数据类型13110,另行添加 对应车速校正
					MessageManager.send(Integer.parseInt(SystemInfo.getSendPORT()), new RequestEntity(jsonObject.toJSONString(),
							checkSpeedType + ""));
					
				} else {
					
					Toast.makeText(getApplicationContext(), "请连接HUD", Toast.LENGTH_SHORT).show();
				}
		
			}
		});

	}
}
